package com.fyzk.activity.chongzhi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.AppToast;
import com.fyzk.bean.JDRecharge;
import com.tyjc.lottery.R;

import http.StringResultBean;

public class CZ_JD extends CZ_BaseActivity {
	

	private final String R_SUCCESS = "success";
	private final String R_FAIL = "fail";
	private final String R_CANCEL = "cancel";

	public static final int PLUGIN_VALID = 0;
    public static final int PLUGIN_NOT_INSTALLED = -1;
    public static final int PLUGIN_NEED_UPGRADE = 2;
	

	@SuppressWarnings("deprecation")
	public void recharge() {
//		int ret = UPPayAssistEx.startPay(this,null, null, mObj_response.result, mMode);
//		if (ret == UPPayAssistEx.PLUGIN_NOT_FOUND) {
//			UPPayAssistEx.installUPPayPlugin(this);
//		}
//		UPPayAssistEx.startPayByJAR(this, PayActivity.class, null, null,
//				mObj_response.result, mMode);
//		int ret = UPPayAssistEx.startPay(this, null, null, mObj_response.result, mMode);
//        if (ret == PLUGIN_NEED_UPGRADE || ret == PLUGIN_NOT_INSTALLED) {
//            // 需要重新安装控件
////            Log.e(LOG_TAG, " plugin not found or need upgrade!!!");
//
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage("完成购买需要安装银联支付控件，是否安装？");
//
//            builder.setNeutralButton("确定",
//                    new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            UPPayAssistEx.installUPPayPlugin(CZ_JD.this);
//                            dialog.dismiss();
//                        }
//                    });
//
//            builder.setPositiveButton("取消",
//                    new DialogInterface.OnClickListener() {
//
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    });
//            builder.create().show();
//
//        }
//        Log.e(LOG_TAG, "" + ret);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}
		String str = data.getExtras().getString("pay_result");
		if (str.equalsIgnoreCase(R_SUCCESS)) {
			AppToast.getToast().show(" 支付成功！ ");
			finish();
		} else if (str.equalsIgnoreCase(R_FAIL)) {
			AppToast.getToast().show(" 支付失败！ ");
		} else if (str.equalsIgnoreCase(R_CANCEL)) {
			AppToast.getToast().show(" 充值行为被打断，请重新充值！ ");
		}
	}


	@Override
	protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
		super.onSuccess(requestCode, resultBean, result);



		if(resultBean.getErrorcode().equals("0")){
			JDRecharge recharge = JSON.parseObject(resultBean.getData(),JDRecharge.class);
			mL_main.removeAllViews();
			WebView web = new WebView(this);
			web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
			//
			web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
			web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
			//
			web.requestFocus();
			web.setVerticalScrollBarEnabled(false);
			web.setHorizontalScrollBarEnabled(false);
			web.getSettings().setJavaScriptEnabled(true);
			web.setWebViewClient(new WeiboWebViewClient());
			web.loadData(recharge.result, "text/html", "utf-8");
			mL_main.addView(web);
		}


	}


	private class WeiboWebViewClient extends WebViewClient {
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			cancleNetProgress();
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			showNetProgress();
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return false;
		}

	}

	@Override
	public void initViewData() {
		super.initViewData();
		zfb_title_rl.setCenterText("京东支付充值");
		tishi1.setText(R.string.yl_tishi1);
		tishi2.setText(R.string.yl_tishi2);
	}

	@Override
	public String initRechargeType() {
		return RECHARGE_JD;
	}

}
